//
//  XZHomeStatusCell.m
//  XZIBeeBo
//
//  Created by Sean on 16/4/10.
//  Copyright © 2016年 XZSean. All rights reserved.
//

#import "XZHomeStatusCell.h"
#import "XZStatusFrame.h"
#import "XZUserModel.h"
#import "XZStatusModel.h"
#import "XZPhoto.h"

#import "XZStatusToolbar.h"
#import "NSString+XZExtension.h"
#import "HWStatusPhotosView.h"
#import "XZIconView.h"


@interface XZHomeStatusCell()
/* 原创微博 */
/** 原创微博整体 */
@property (nonatomic, weak) UIView *originalView;
/** 头像 */
@property (nonatomic, weak) XZIconView *iconView;
/** 会员图标 */
@property (nonatomic, weak) UIImageView *vipView;
/** 配图 */
@property (nonatomic, weak) HWStatusPhotosView *photoView;
/** 昵称 */
@property (nonatomic, weak) UILabel *nameLabel;
/** 时间 */
@property (nonatomic, weak) UILabel *timeLabel;
/** 来源 */
@property (nonatomic, weak) UILabel *sourceLabel;
/** 正文 */
@property (nonatomic, weak) UILabel *contentLabel;

/* 转发微博 */
/** 转发微博整体 */
@property (nonatomic, weak) UIView *retweetView;
/** 转发微博正文 + 正文 */
@property (nonatomic, weak) UILabel *retweetcontentLabel;
/** 转发配图 */
@property (nonatomic, weak) HWStatusPhotosView *retweetphotoView;

/* 工具条 */
@property (nonatomic, weak) XZStatusToolbar *toolBar;
@end


@implementation XZHomeStatusCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{
   static NSString *CellID = @"StatusCell";
    XZHomeStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (!cell) {
        cell = [[XZHomeStatusCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellID];
    }
    return cell;
}
/**
 *  cell的初始化方法，一个cell只会调用一次
 *  一般在这里添加所有可能显示的子控件，以及子控件的一次性设置
 */
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
       
         self.backgroundColor = XZColor(211, 211, 211);
        
        /** 原创微博整体 */
        [self setOriginalView];
        /** 转发微博整体 */
        [self setRetweetView];
        
        //初始化工具条
        [self setUpToolBar];
        
        
        
        
         
    }
 
    return self;
}

- (void)setStatusFrame:(XZStatusFrame *)statusFrame{

    _statusFrame = statusFrame;
   
   
    /** 原创微博整体 */
    self.originalView.frame = statusFrame.originalViewF;
    
    /** 头像 */
    self.iconView.frame = statusFrame.iconViewF;
    self.iconView.user = statusFrame.status.user;
    //[self.iconView sd_setImageWithURL:[NSURL URLWithString:statusFrame.status.user.profile_image_url] placeholderImage:[UIImage imageNamed:@"avatar_default"]];
    
    /** 会员图标 */
    if (statusFrame.status.user.isVip) {
        self.vipView.hidden = NO;
        
        self.vipView.frame = statusFrame.vipViewF;
        NSString *vipName = [NSString stringWithFormat:@"common_icon_membership_level%d", statusFrame.status.user.mbrank];
        self.vipView.image = [UIImage imageNamed:vipName];
        
        self.nameLabel.textColor = [UIColor orangeColor];
    } else {
        self.nameLabel.textColor = [UIColor blackColor];
        self.vipView.hidden = YES;
    }

   
    /** 配图 */
    if (statusFrame.status.pic_urls.count) { //有缩略图
        self.photoView.frame = statusFrame.photoViewF;
        self.photoView.photos = statusFrame.status.pic_urls;
        
        
        
       //XZPhoto *photo =  [statusFrame.status.pic_urls lastObject];
       //[self.photoView sd_setImageWithURL:[NSURL URLWithString:photo.thumbnail_pic] placeholderImage:[UIImage imageNamed:@"timeline_image_placeholder"]];
        self.photoView.hidden = NO;
    } else {
        self.photoView.hidden = YES;
    }
   
   
    /** 昵称 */
    self.nameLabel.frame = statusFrame.nameLabelF;
    self.nameLabel.text = statusFrame.status.user.name;
    
//    /** 时间 */
//    self.timeLabel.text = statusFrame.status.created_at;
//    self.timeLabel.frame = statusFrame.timeLabelF;
//    self.timeLabel.textColor = [UIColor orangeColor];
//    /** 来源 */
//    self.sourceLabel.text = statusFrame.status.source;
//    self.sourceLabel.frame = statusFrame.sourceLabelF;
    /** 时间 */
    NSString *time = statusFrame.status.created_at;
    CGFloat timeX = statusFrame.nameLabelF.origin.x;
    CGFloat timeY = CGRectGetMaxY(statusFrame.nameLabelF) + XZStatusCellBorderW;
    CGSize timeSize = [time sizeWithFont:HWStatusCellTimeFont];
    self.timeLabel.frame = (CGRect){{timeX, timeY}, timeSize};
    self.timeLabel.text = time;
    self.timeLabel.textColor = [UIColor orangeColor];

    /** 来源 */
    CGFloat sourceX = CGRectGetMaxX(self.timeLabel.frame) + XZStatusCellBorderW;
    CGFloat sourceY = timeY;
    CGSize sourceSize = [statusFrame.status.source sizeWithFont:HWStatusCellSourceFont];
    self.sourceLabel.frame = (CGRect){{sourceX, sourceY}, sourceSize};
    self.sourceLabel.text = statusFrame.status.source;

    
    /** 正文 */
    self.contentLabel.text = statusFrame.status.text;
    self.contentLabel.frame = statusFrame.contentLabelF;

    
    
    
    /** 转发微博整体 */
     /** 被转发微博整体 */
    if (statusFrame.status.retweeted_status) {
        
        
        XZStatusModel *retweeted_status = statusFrame.status;
        XZUserModel *retweeted_status_user = retweeted_status.user;
        
        /** 被转发微博整体 */
        self.retweetView.hidden = NO;
        self.retweetView.frame = statusFrame.retweetViewF;
        

        //拼接文字
        NSString *retweetConten = [NSString stringWithFormat:@"@%@ : %@ ",retweeted_status_user.name,retweeted_status.text];
        
        /**被转发微博正文 */
        self.retweetcontentLabel.text = retweetConten;
        self.retweetcontentLabel.frame = statusFrame.retweetcontentLabelF;
        
        /** 被转发微博配图 */
        if (statusFrame.status.pic_urls.count) { //有缩略图
            
            self.retweetphotoView.frame = statusFrame.retweetphotoViewF;
            self.retweetphotoView.photos = statusFrame.status.pic_urls;
          
            // XZPhoto *retweetPhoto =  [retweeted_status.pic_urls firstObject];
            //[self.retweetphotoView sd_setImageWithURL:[NSURL URLWithString:retweetPhoto.thumbnail_pic] placeholderImage:[UIImage imageNamed:@"timeline_image_placeholder"]];
        
            self.retweetphotoView.hidden = NO;
        } else {
            self.retweetphotoView.hidden = YES;
        }
   
 
    }else {
    
        self.retweetView.hidden = YES;
      }
    
    
    
#warning mark---设置工具条数据显示不对
    //设置工具条
    self.toolBar.frame = statusFrame.toolBarF;
     //XZStatusToolbar *tooBar = [XZStatusToolbar tooBar];
    //tooBar.statusView = statusFrame.status;
    self.statusFrame.status = statusFrame.status;
    
}
 /** 原创微博整体 */
- (void)setOriginalView{

    
    /** 原创微博整体 */
    UIView *originalView = [[UIView alloc] init];
    [self.contentView addSubview:originalView];
    self.originalView = originalView;
    
    self.originalView.backgroundColor = [UIColor whiteColor];
    
    /** 头像 */
    XZIconView *iconView = [[XZIconView alloc] init];
    [originalView addSubview:iconView];
  //  XZLog(@"======");
    //iconView.layer.masksToBounds = YES;
    //iconView.layer.cornerRadius = 10;
    
    self.iconView = iconView;
    
    /** 会员图标 */
    UIImageView *vipView = [[UIImageView alloc] init];
    [originalView addSubview:vipView];
    vipView.contentMode = UIViewContentModeCenter;
    self.vipView = vipView;
    
    /** 配图 */
    HWStatusPhotosView *photoView = [[HWStatusPhotosView alloc] init];
    [originalView addSubview:photoView];
    self.photoView = photoView;
    
    /** 昵称 */
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.font = HWStatusCellNameFont;
    [originalView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    /** 时间 */
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.font = HWStatusCellTimeFont;
    [originalView addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    /** 来源 */
    UILabel *sourceLabel = [[UILabel alloc] init];
    sourceLabel.font = HWStatusCellSourceFont;
    [originalView addSubview:sourceLabel];
    self.sourceLabel = sourceLabel;
    
    /** 正文 */
    UILabel *contentLabel = [[UILabel alloc] init];
    contentLabel.font = HWStatusCellContentFont;
    contentLabel.numberOfLines = 0;
    [originalView addSubview:contentLabel];
    self.contentLabel = contentLabel;

}
 /** 转发微博整体 */
- (void)setRetweetView{

    /** 转发微博整体 */
    UIView *retweetView = [[UIView alloc] init];
    [self.contentView addSubview:retweetView];
    self.retweetView = retweetView;
    self.retweetView.backgroundColor = XZColor(247, 247, 247);
    
    ///** 转发微博正文 + 正文 */
    UILabel *retweetcontentLabel = [[UILabel alloc] init];
    [retweetView addSubview:retweetcontentLabel];
    retweetcontentLabel.font = HWStatusCellRetweetContentFont;
    retweetcontentLabel.numberOfLines = 0;
    self.retweetcontentLabel =  retweetcontentLabel;
   // retweetcontentLabel.backgroundColor = XZRandomColor;
    
    
    /** 转发配图 */
    HWStatusPhotosView *retweetphotoView = [[HWStatusPhotosView alloc] init];
    [retweetView addSubview:retweetphotoView];
    self.retweetphotoView = retweetphotoView;
    
  
}
/**
 *  添加工具条
 */
- (void)setUpToolBar{
   
    XZStatusToolbar *toolBar = [XZStatusToolbar tooBar];
    [self.contentView addSubview:toolBar];
    self.toolBar = toolBar;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
